package com.wswy.wzcx.module

import android.content.Context
import com.che.libcommon.utils.optional.Optional
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.model.AdConfigs
import com.wswy.wzcx.model.TabConfResp
import com.wswy.wzcx.model.conf.SysConfMap
import com.wswy.wzcx.model.news.NewsCategory
import com.wswy.wzcx.model.topic.TopicTagType
import com.wswy.wzcx.model.weather.CarTips
import com.wswy.wzcx.utils.fromJson
import io.reactivex.Single


/**
 * 首页新闻分类配置
 */
class HomeNewsCategoryLoader : AbsCacheLoader<List<NewsCategory>?>(AppContext.getContext(),"news_categories.json") {

    override fun convert(json: String): List<NewsCategory>? {
        return fromJson(json)
    }

    override fun loadFromApi(): Single<Optional<List<NewsCategory>?>> {
        return Api.get().newsCategories
    }


    override fun getKey(): String = "home_news_category"
}


/**
 * 首页配置
 */
class NewsTabCategoryLoader : AbsCacheLoader<List<NewsCategory>?>(AppContext.getContext(),"config/tab_news_categories.json") {

    override fun convert(json: String): List<NewsCategory>? {
        return fromJson(json)
    }

    override fun loadFromApi(): Single<Optional<List<NewsCategory>?>> {
        return Api.get().getNewsCategories("newsList")
    }


    override fun getKey(): String = "news_tab_category"
}


/**
 * 系统配置
 */
class SysConfLoader(context: Context) :AbsCacheLoader<SysConfMap?>(context,"config/sys_conf.json") {
    override fun loadFromApi(): Single<Optional<SysConfMap?>> =  Api.get().sysConf

    override fun convert(json: String): SysConfMap?  = fromJson(json)

    override fun getKey(): String = "app_sys_config"
}

/**
 * 全局广告位配置
 */
class AllAdConfLoader(context: Context) :AbsCacheLoader<AdConfigs?>(context,"config/ad_conf.json") {
    override fun loadFromApi(): Single<Optional<AdConfigs?>> =  Api.get().allAdConfig

    override fun convert(json: String): AdConfigs?  = fromJson(json)

    override fun getKey(): String = "app_all_ad_conf"

    //override fun compareData(): Boolean  = true
}


/**
 * 栏目配置
 */
class TabConfLoader(context:Context) : AbsCacheLoader<TabConfResp?>(context) {
    override fun loadFromApi(): Single<Optional<TabConfResp?>> = Api.get().tabConfigs

    override fun convert(json: String): TabConfResp? = fromJson(json)

    override fun getKey(): String = "tab_configs"
}


/**
 * 栏目配置
 */
class TopicTagsLoader(context:Context) : AbsCacheLoader<List<TopicTagType>?>(context) {
    override fun loadFromApi(): Single<Optional<List<TopicTagType>?>> = Api.get().topicTags

    override fun convert(json: String): List<TopicTagType>? = fromJson(json)

    override fun getKey(): String = "topic_tags"
}

/**
 * 养车小技巧
 */
class CarTipsLoader(context:Context) : AbsCacheLoader<Map<String,List<CarTips>>?>(context,"config/car_tips.json") {
    override fun loadFromApi(): Single<Optional<Map<String,List<CarTips>>?>> = Api.get().carTips

    override fun convert(json: String): Map<String,List<CarTips>>? = fromJson(json)

    override fun getKey(): String = "car_tips_help"
}